package com.cn.web.controller.Equipment;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.vo.Equipment.BusinessDimensionConfiguration;
import com.cn.bean.vo.Equipment.EquipmentStructure;
import com.cn.bean.vo.PartsManagement.TeamPersonnel;
import com.cn.config.config.Result;
import com.cn.service.server.Equipment.BusinessDimensionConfigurationService;
import com.cn.service.server.Equipment.EquipmentStructureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/Bsc")
@Transactional
public class BusinessDimensionConfigurationController {
    private final BusinessDimensionConfigurationService businessDimensionConfigurationService;

    @Autowired(required = false)
    public BusinessDimensionConfigurationController(BusinessDimensionConfigurationService businessDimensionConfigurationService) {
        this.businessDimensionConfigurationService = businessDimensionConfigurationService;

    }

    @RequestMapping("/select/BusinessDimensionConfiguration")
    @ResponseBody
    public List<BusinessDimensionConfiguration> selectBusinessDimensionConfiguration(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String dimensionCode = jsonObject.getString("dimensionCode");
        String dimensionName = jsonObject.getString("dimensionName");
        dimensionCode = (dimensionCode != null) ? dimensionCode : "";
        dimensionName = (dimensionName != null) ? dimensionName : "";
        return businessDimensionConfigurationService.selectBusinessDimensionConfiguration(limit, page, dimensionCode, dimensionName);
    }

    @RequestMapping("/add/BusinessDimensionConfiguration")
    @ResponseBody
    public Result addBusinessDimensionConfiguration(@RequestBody JSONObject jsonObject) {
//        Integer ID = jsonObject.getInteger("ID");
        String dimensionName = jsonObject.getString("dimensionName");
        Integer status = jsonObject.getInteger("status");
        Integer dimensionType = jsonObject.getInteger("dimensionType");
        String description = jsonObject.getString("description");
        JSONArray childList = jsonObject.getJSONArray("childList");
        if (childList.size() == 0) {
            return Result.failure("500", "请选择维度项目");
        }
        List<BusinessDimensionConfiguration> bus = new ArrayList<>();
        for (int i = 0; i < childList.size(); i++) {
            JSONObject childObject = childList.getJSONObject(i);
            // 从 childObject 中获取 TeamPersonnel 对象的属性
            int dimensionItemID = childObject.getInteger("dimensionItemID");
            String dimensionItemName = childObject.getString("dimensionItemName");
            BusinessDimensionConfiguration businessDimensionConfiguration = new BusinessDimensionConfiguration();
            businessDimensionConfiguration.setDimensionItemID(dimensionItemID);
            businessDimensionConfiguration.setDimensionItemName(dimensionItemName);
            bus.add(businessDimensionConfiguration);
        }

        return businessDimensionConfigurationService.addBusinessDimensionConfiguration(dimensionType, dimensionName, status, description, bus);
    }
    @RequestMapping("/delete/BusinessDimensionConfiguration")
    @ResponseBody
    public Result deleteBusinessDimensionConfiguration(@RequestBody JSONObject jsonObject) {
        JSONArray deleteIdList = jsonObject.getJSONArray("deleteIdList");
        List<BusinessDimensionConfiguration> idList = new ArrayList<>();
        for (int i = 0; i < deleteIdList.size(); i++) {
            JSONObject deleteIdObject = deleteIdList.getJSONObject(i);
            int id = deleteIdObject.getInteger("id");
            BusinessDimensionConfiguration businessDimensionConfiguration = new BusinessDimensionConfiguration();
            businessDimensionConfiguration.setID(id);
            idList.add(businessDimensionConfiguration);
        }
        return businessDimensionConfigurationService.deleteBusinessDimensionConfiguration(idList);
    }
    @RequestMapping("/select/BusinessDimensionConfiguration/Child")
    @ResponseBody
    public List<BusinessDimensionConfiguration> selectBusinessDimensionConfigurationChild(@RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("ID");
        return businessDimensionConfigurationService.selectBusinessDimensionConfigurationChild(id);
    }

    @RequestMapping("/update/BusinessDimensionConfiguration")
    @ResponseBody
    public Result updateBusinessDimensionConfiguration(@RequestBody JSONObject jsonObject) {
        Integer ID = jsonObject.getInteger("ID");
        String dimensionName = jsonObject.getString("dimensionName");
        Integer status = jsonObject.getInteger("status");
        Integer dimensionType = jsonObject.getInteger("dimensionType");
        String description = jsonObject.getString("description");
        JSONArray childList = jsonObject.getJSONArray("childList");
        if (childList.size() == 0) {
            return Result.failure("500", "请选择维度项目");
        }
        List<BusinessDimensionConfiguration> bus = new ArrayList<>();
        for (int i = 0; i < childList.size(); i++) {
            JSONObject childObject = childList.getJSONObject(i);
            // 从 childObject 中获取 TeamPersonnel 对象的属性
            int dimensionItemID = childObject.getInteger("dimensionItemID");
            String dimensionItemName = childObject.getString("dimensionItemName");
            BusinessDimensionConfiguration businessDimensionConfiguration = new BusinessDimensionConfiguration();
            businessDimensionConfiguration.setDimensionItemID(dimensionItemID);
            businessDimensionConfiguration.setDimensionItemName(dimensionItemName);
            bus.add(businessDimensionConfiguration);
        }

        return businessDimensionConfigurationService.updateBusinessDimensionConfiguration(ID,dimensionType,dimensionName,status,description,bus);
    }
}
